'use strict';

    angular.module('app')
        .controller('createCategoryController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
                                             function($scope, $rootScope, $state, $timeout, toaster) {
        	$scope.loading = false;
        	$scope.record = {
        		biHouseinfo:{},
        		id : $state.params.id
        	};
        	
        	if($state.includes('**.update')){
                $scope.title="修改设备类型";
                $scope.reset = false;//修改不允许重置
                var id = $state.params.id;
                activate(id);
                validate();
            }else if($state.includes('**.create')){
            	$scope.title="新增设备类型";
            	$scope.reset = true;//新增时可以重置
                validate();
            }
        	//重置表单
			$scope.resetFrom = function(){
				$scope.record = {
	        		biHouseinfo:{},
	        		id : $state.params.id
        		};
			}
        	$scope.submit= function(){
                var m = $scope.record;
                if($scope.record.id !=null){//id非空代表着是修改
                	if(m){
                        $scope.isDisabled = true;
                        $.ajax({
            				type: 'POST',
            	            dataType: 'json',
            				contentType:'application/json;charset=UTF-8',
            				url : '/dac/dm/category/update',
            				data: angular.toJson($scope.record)
            			}).then(function(result){
                            if(result.httpCode ==200){//成功
                            toaster.clear('*');
                            toaster.pop('success', '', "保存成功");
                            $timeout(function(){
                                $state.go("main.dm.category.list", $state.params);
                            },2000);
                        }else{
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                            $scope.isDisabled = false;
                        }
                        });
                    }
                   
                }else{//id为空代表着是新增
                	if(m){
                        $scope.isDisabled = true;
                        $.ajax({
            				type: 'POST',
            	            dataType: 'json',
            				contentType:'application/json;charset=UTF-8',
            				url : '/dac/dm/category/create',
            				data: angular.toJson($scope.record)
            			}).then(function(result){
                            if(result.httpCode ==200){//成功
                                toaster.clear('*');
                                toaster.pop('success', '', "保存成功");
                                $timeout(function(){
                                $state.go("main.dm.category.list", $state.params);
                            },2000);
                            }else{
                                toaster.clear('*');
                                toaster.pop('error', '', result.msg);
                                $scope.isDisabled = false;
                            }
                        });
                    }
                    
                }
                
            }
        	
        	//查询设备类型列表
        	$scope.loadDeviceCategoryList = function () {
        		$scope.bparam = {};
        		$scope.bparam.nopage = '1';
        		$.ajax({
    				type: 'POST',
    	            dataType: 'json',
    				contentType:'application/json;charset=UTF-8',
    				url : '/dac/dm/category/categoryList',
    				data: angular.toJson($scope.bparam)
    			}).then(function(result) {
    				if (result.httpCode == 200) {
    					$scope.deviceCategory = result.data;
    					$scope.$apply();
    				} 
    				
    			});
        	}
        	$scope.loadDeviceCategoryList();
        	
        	// 初始化页面
            function activate(id) {
    	        $scope.loading = true;
            	$.ajax({
            		type: 'PUT',
    	            dataType: 'json',
    				contentType:'application/json;charset=UTF-8',
    				url : '/dac/dm/category/deviceCategory',
    				data: angular.toJson({'id': id})
    			}).then(function(result) {
    		        $scope.loading = false;
    				if (result.httpCode == 200) {
    					$scope.record = result.data;
    					$scope.record.id = id;//给id值，因为后台需要根据是否有id值来操作：新增（无id）或者修改（有id）
    					$scope.record.categoryType = $scope.record.categoryType+'';
    				} else {
    					$scope.msg = result.msg;
    				}
    				$scope.$apply();
    			});
            }
        	
          //表单校验
        	function validate(){
                jQuery('form').validate({
                	rules: {
                		categoryName: {
                    		required: true
                    	},
                		categoryType: {
                    		required: true
                    	}
                    },
                    messages: {
                    	categoryName: {
                    		required: '请选择类型名称'
                    	},
                    	categoryType: {
                    		required: '请选择设备分类'
                    	}
                    },
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }

    }]);